import GoEasy from 'goeasy'

export function openGoEasyConnect() {
  const userInfo = uni.getStorageSync('userInfo') || {}
  const userId = userInfo.id || 0
  if (!userId) {
    console.log('用户未登录，无法连接 GoEasy')
    return null
  }
  let goEasy = GoEasy.instance
  if (!goEasy) {
    console.log('GoEasy instance create')
    goEasy = GoEasy.getInstance({
      host: 'hangzhou.goeasy.io', //新加坡host：singapore.goeasy.io
      appkey: 'BC-6a538db22cce4bbd812a83fb8558ceed', //替换为您的应用appkey
      modules: ['im']
    })
  }
  let connectionStatus = goEasy.getConnectionStatus()
  console.log('GoEasy 当前连接状态：' + connectionStatus)
  if (connectionStatus != 'disconnected') {
    console.log('已连接 GoEasy')
    return
  }
  goEasy.connect({
    id: userId,
    data: {
      avatar: uni.getStorageSync('userInfo').avatar,
      nickname: uni.getStorageSync('userInfo').nickname
    },
    onSuccess: function () {
      console.log('GoEasy connect successfully.')
    },
    onFailed: function (error) {
      console.log('Failed to connect GoEasy, code: ' + error.code + ',error:' + error.content)
    },
    onProgress: function (attempts) {
      console.log('GoEasy is connecting', attempts)
    }
  })
  return goEasy
}
